package com.ruoyi.project.channel.businessCapital.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.channel.businessCashChange.domain.BusinessCashChange;
import com.ruoyi.project.channel.businessCashChange.service.IBusinessCashChangeService;
import com.ruoyi.project.channel.vwBusinessCashStat.domain.VwBusinessCashStat;
import com.ruoyi.project.channel.vwBusinessCashStat.service.IVwBusinessCashStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.channel.businessCapital.mapper.BusinessCapitalMapper;
import com.ruoyi.project.channel.businessCapital.domain.BusinessCapital;
import com.ruoyi.project.channel.businessCapital.service.IBusinessCapitalService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 商户提现 服务层实现
 * 
 * @author zero
 * @date 2019-05-04
 */
@Service
public class BusinessCapitalServiceImpl implements IBusinessCapitalService 
{
	@Autowired
	private BusinessCapitalMapper businessCapitalMapper;
	@Autowired
	private IBusinessCashChangeService businessCashChangeService;
	@Autowired
	private IVwBusinessCashStatService vwBusinessCashStatService;

	/**
     * 查询商户提现信息
     * 
     * @param capitalId 商户提现ID
     * @return 商户提现信息
     */
    @Override
	public BusinessCapital selectBusinessCapitalById(String capitalId)
	{
	    return businessCapitalMapper.selectBusinessCapitalById(capitalId);
	}
	
	/**
     * 查询商户提现列表
     * 
     * @param businessCapital 商户提现信息
     * @return 商户提现集合
     */
	@Override
	public List<BusinessCapital> selectBusinessCapitalList(BusinessCapital businessCapital)
	{
	    return businessCapitalMapper.selectBusinessCapitalList(businessCapital);
	}
	
    /**
     * 新增商户提现
     * 
     * @param businessCapital 商户提现信息
     * @return 结果
     */
	@Override
	public int insertBusinessCapital(BusinessCapital businessCapital)
	{
		// 记录资金变更记录
		BusinessCashChange change = new BusinessCashChange();
		change.setAmount(businessCapital.getAmount());
		change.setBusinessId(businessCapital.getBusinessId());
		change.setOpType("2");
		VwBusinessCashStat searchState = new VwBusinessCashStat();
		searchState.setBusinessId(businessCapital.getBusinessId());
		List<VwBusinessCashStat> stats = vwBusinessCashStatService.selectVwBusinessCashStatList(searchState);
		if(stats.size() == 0){
			change.setPreAmount(BigDecimal.ZERO);
		}else{
			change.setPreAmount(stats.get(0).getAmountSum());
		}
		change.setNextAmount(change.getPreAmount().subtract(change.getAmount()));
		change.setCreateTime(new Date());
		change.setRemark("申请提现");
		// 插入提现信息一定要放在这里, 否则如果入库 则统计出来的数据就变了
		businessCapitalMapper.insertBusinessCapital(businessCapital);
	    return businessCashChangeService.insertBusinessCashChange(change);
	}
	
	/**
     * 修改商户提现
     * 
     * @param businessCapital 商户提现信息
     * @return 结果
     */
	@Override
	public int updateBusinessCapital(BusinessCapital businessCapital)
	{
	    return businessCapitalMapper.updateBusinessCapital(businessCapital);
	}

	/**
	 * 状态变更
	 * @param status
	 * @param id
	 * @return
	 */
	public AjaxResult changeStatus(String status,String id){
		BusinessCapital businessCapital = selectBusinessCapitalById(id);
		if(businessCapital.getStatus().equals("3")){
			return AjaxResult.error("已审核通过了,不能再进行别的操作!");
		}

		if(businessCapital.getStatus().equals("2") && status.equals("3") ){
			return AjaxResult.error("被驳回状态是不能直接设置已打款的!");
		}
		// 记录资金变更记录
		BusinessCashChange change = new BusinessCashChange();
		change.setAmount(businessCapital.getAmount());
		change.setBusinessId(businessCapital.getBusinessId());
		VwBusinessCashStat searchState = new VwBusinessCashStat();
		searchState.setBusinessId(businessCapital.getBusinessId());
		List<VwBusinessCashStat> stats = vwBusinessCashStatService.selectVwBusinessCashStatList(searchState);
		if(stats.size() == 0){
			change.setPreAmount(BigDecimal.ZERO);
		}else{
			change.setPreAmount(stats.get(0).getAmountSum());
		}
		change.setCreateTime(new Date());
		if(businessCapital.getStatus().equals("1") && status.equals("3")) {
			change.setRemark("放款");
			change.setOpType("3");
			change.setAmount(BigDecimal.ZERO);
			change.setNextAmount(change.getPreAmount());
		}else if(businessCapital.getStatus().equals("1") && status.equals("2")) {
			change.setRemark("提现驳回");
			change.setOpType("8");
			// 冻结金额取消 可提现资金增加
			change.setNextAmount(change.getPreAmount().add(change.getAmount()));
		}
		businessCapital.setApprovalTime(new Date());
		businessCapital.setStatus(status);
		updateBusinessCapital(businessCapital);
		businessCashChangeService.insertBusinessCashChange(change);
		return AjaxResult.success("success");
	}
	/**
     * 删除商户提现对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteBusinessCapitalByIds(String ids)
	{
		return businessCapitalMapper.deleteBusinessCapitalByIds(Convert.toStrArray(ids));
	}
	
}
